from python.common.config import DB_URL
import requests


def fetch(action, body=None):
    if body is None:
        body = {}
    url = f"{DB_URL}/{action}"
    try:
        response = requests.post(url, json=body)
        json = response.json()
        if response.status_code == 200:
            if 'data' in json:
                return json['data']
            else:
                return json.get('message', '')
        else:
            code = response.status_code
            return f"错误码：{code}"
    except ValueError:
        return '解析错误'
    except requests.RequestException as e:
        return str(e)

